package my_a10bbc;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.TextView;
import android.widget.Toast;


import com.google.gson.Gson;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;

import my_a10bbc.cn.xp.com.adapter.IEssayAdapter;
import my_a10bbc.cn.xp.com.adapter.IHPAdapter;
import my_a10bbc.cn.xp.com.adapter.IMusicAdapter;
import my_a10bbc.cn.xp.com.adapter.IQuesAdapter;
import my_a10bbc.cn.xp.com.adapter.ISerialAdapter;
import my_a10bbc.cn.xp.com.bean.IEssayBean;
import my_a10bbc.cn.xp.com.bean.IHPBean;
import my_a10bbc.cn.xp.com.bean.IMusicBean;
import my_a10bbc.cn.xp.com.bean.IQuesBean;
import my_a10bbc.cn.xp.com.bean.ISerialBean;
import sdstcom.lbq.one.R;

public class InfolistActivity extends AppCompatActivity implements IEssayAdapter.MyOnItemclick,IHPAdapter.MyOnItemclick,IMusicAdapter.MyOnItemclick,IQuesAdapter.MyOnItemclick,ISerialAdapter.MyOnItemclick{
    private RecyclerView rv;
    private Toolbar tb;
    private TextView tv;


    private ArrayList<IEssayBean.DataBean> list_essay;
    private ArrayList<IHPBean.DataBean> list_hp;
    private ArrayList<ISerialBean.DataBean> list_serial;
    private ArrayList<IQuesBean.DataBean> list_ques;
    private ArrayList<IMusicBean.DataBean> list_music;




    private IHPAdapter adapter_hp;
    private ISerialAdapter adapter_serial;
    private IQuesAdapter adapter_ques;
    private IMusicAdapter adapter_music;
    private IEssayAdapter adapter_essay;
    private static String url_info="/bymonth/";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_infolist);
        init();
    }

    private void init() {
        tv=(TextView)findViewById(R.id.tv_infolist);
        tb=(Toolbar)findViewById(R.id.tb_infolist);
        tb.setNavigationIcon(R.mipmap.ic_keyboard_backspace_black_24dp);
        tb.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                exit();
            }
        });




        rv=(RecyclerView)findViewById(R.id.rv_infolist);
        list_essay=new ArrayList<>();
        list_hp=new ArrayList<>();
        list_music=new ArrayList<>();
        list_ques=new ArrayList<>();
        list_serial=new ArrayList<>();

        adapter_essay=new IEssayAdapter(this,list_essay);
        adapter_hp=new IHPAdapter(this,list_hp);
        adapter_music=new IMusicAdapter(this,list_music);
        adapter_ques=new IQuesAdapter(this,list_ques);
        adapter_serial=new ISerialAdapter(this,list_serial);

        adapter_hp.setOnItemClick(this);
        adapter_essay.setOnItemClick(this);
        adapter_music.setOnItemClick(this);
        adapter_ques.setOnItemClick(this);
        adapter_serial.setOnItemClick(this);




        lodaData();
    }

    private void exit() {
        finish();
        overridePendingTransition(R.anim.in_from_left, R.anim.out_to_right);

    }

    @Override
    public void onItemClick(int index) {
        Toast.makeText(InfolistActivity.this, ""+index, Toast.LENGTH_SHORT).show();
    }
    private void lodaData(){
        final LinearLayoutManager manager=new LinearLayoutManager(this);
        final GridLayoutManager manager1=new GridLayoutManager(this,2);
        Intent intent=getIntent();
        final String type=intent.getStringExtra("type");
        String date=intent.getStringExtra("date");
        String title=intent.getStringExtra("title");
        tv.setText(title);








//        Log.e("InfolistActivity",type+date );
        RequestParams params=new RequestParams(ContentValue.URL_INFOLIST_BASE+type+url_info+date+ContentValue.URL_INFOLIST_LAST);
//        RequestParams params=new RequestParams(ContentValue.URL_INFOLIST_BASE+"essay"+url_info+"2017-01-01"+ContentValue.URL_INFOLIST_LAST);
        x.http().get(params, new Callback.CommonCallback<String>() {


            @Override
            public void onSuccess(String result) {
                       Log.e("InfolistActivity",result );

                switch (type){
                    case "hp":

                        IHPBean ihpBean=new Gson().fromJson(result,IHPBean.class);
                        list_hp.clear();
                        list_hp.addAll(ihpBean.data);
                        Log.e("InfolistActivity",list_hp.get(0).hp_img_url );
                        adapter_hp.notifyDataSetChanged();
                        rv.setLayoutManager(manager1);
                        rv.setAdapter(adapter_hp);


                        break;
                    case "essay":
                        IEssayBean essayBean=new Gson().fromJson(result,IEssayBean.class);
                        list_essay.addAll(essayBean.data);
                        list_essay.clear();
                        list_essay.addAll(essayBean.data);
                        adapter_essay.notifyDataSetChanged();

                        rv.setLayoutManager(manager);
                        rv.setAdapter(adapter_essay);
                        break;
                    case "question":
                        IQuesBean quesBean=new Gson().fromJson(result,IQuesBean.class);
                        list_ques.clear();
                        list_ques.addAll(quesBean.data);
                        adapter_ques.notifyDataSetChanged();

                        rv.setLayoutManager(manager);
                        rv.setAdapter(adapter_ques);
                        break;
                    case "serialcontent":
                        ISerialBean serialBean=new Gson().fromJson(result,ISerialBean.class);
                        list_serial.clear();
                        list_serial.addAll(serialBean.data);
                        adapter_serial.notifyDataSetChanged();
                        rv.setLayoutManager(manager);
                        rv.setAdapter(adapter_serial);
                        break;
                    case "music":
                        IMusicBean musicBean=new Gson().fromJson(result,IMusicBean.class);

                        list_music.clear();
                        list_music.addAll(musicBean.data);
                        Log.e("InfolistActivity",list_music.get(0).cover );
                        adapter_music.notifyDataSetChanged();
                        rv.setLayoutManager(manager);
                        rv.setAdapter(adapter_music);
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });

    }
}
